var ecommRecordEditCtrl  = new XFApp.client();
$.views.converters({
	
});

XFApp.client.prototype.ecommRecordEdit = {
		successCode : 10000,	
		ecommObj : null,
		urlItems : {			
			saveStoreURL: basePath+"/userCenter/ecommerceRecord/save",
			submitStoreURL: basePath+"/userCenter/ecommerceRecord/submit",
			updateStoreURL: basePath+"/userCenter/ecommerceRecord/update"
		},
		
		formValidate : function(){
			$("#editForm").validate({
		        rules: {
		        	customerCode: "required",
		        	customerName: "required",
		        	agentCode: "required",
		        	agentName: "required",
		        	appUId: "required",
		        	appUName: "required",
		        	ebpCode: "required",
		        	ebpName: "required",
		        	ebcCode: "required",
		        	ebcName: "required",
		        	classifyCode: "required",
		        	classifyName: "required",
		        	interfaceCode: "required",
		        	license:"required"
		        	//imagePath: "required"
				  },
				 messages: {
					 customerCode: "请填写客户代码",
			         customerName: "请填写企业名称",
			         agentCode: "请填写申报类企业代码",
			         agentName: "请填写申报类企业名称",
			         appUId: "请填写持卡人编号",
			         appUName: "请填写持卡人姓名",
			         ebpCode: "请填写电商平台代码",
			         ebpName: "请填写电商平台名称",
			         ebcCode: "请填写电商企业代码",
			         ebcName: "请填写电商企业名称",
			         classifyCode: "请填写预归类企业代码",
			         classifyName: "请填写预归类企业名称",
			         interfaceCode: "请填写线上订单接口编号",
			         license:"请填写营业执照号"
			         //imagePath: "请上传海关备案证书图片"		        	    
			         }
		    });
			$("#editForm").validate();
		},
		
		event : function (){			
			_this = this;
			
			$("#saveBtn").on("click",function(){				
				if($("#editForm").validate().form()){
					ecommRecordEditCtrl.ecommRecordEdit.submit(0,_this.urlItems.saveStoreURL);
				}				
			});
			$("#submitBtn").on("click",function(){				
				if($("#editForm").validate().form()){
					ecommRecordEditCtrl.ecommRecordEdit.submit(1,_this.urlItems.submitStoreURL);
				}				
			});
			$("#submitUpdateBtn").on("click",function(){				
				if($("#editForm").validate().form()){
					ecommRecordEditCtrl.ecommRecordEdit.submit(2,_this.urlItems.updateStoreURL);
				}				
			});
			
			//表单验证
			_this.formValidate();	
		},		
		
		
		submit : function (operateStatus,url){		    
			var busiDirtVal;
			
			$("#inputDiv input[type='radio']").each(function(){
				if($(this).prop("checked")){
					busiDirtVal = $(this).val();
				}				   
			});			
			var entity = {
				id:$("#id").val(),
				customerCode : $("#customerCode").val(),
				customerName : $("#customerName").val(),
				companyAddr : $("#companyAddr").val(),
				postCode : $("#postCode").val(),
				busiIndt : $("#busiIndt").val(),
				busiDirt : busiDirtVal,
				salePrct : $("#salePrct").val(),
				purchPrct : $("#purchPrct").val(),
				license : $("#license").val(),
				agentCode : $("#agentCode").val(),
				agentName : $("#agentName").val(),
				appUId : $("#appUId").val(),
				appUName : $("#appUName").val(),
				ebpCode : $("#ebpCode").val(),
				ebpName : $("#ebpName").val(),				
				ebcCode : $("#ebcCode").val(),
				ebcName : $("#ebcName").val(),
				classifyCode : $("#classifyCode").val(),
				classifyName : $("#classifyName").val(),
				interfaceCode : $("#interfaceCode").val()
			}			
			var modifyFlag = false;
			if(!!this.ecommObj){
				//没有修过需要上传的文件
				if(!$("#licensePath").val() && !$("#certPath").val()){
					//输入框内容没有修改
					var flag = false;
					for(var pName in entity){
						if(this.ecommObj[pName]!=entity[pName]){
							flag = true;
							break;
						}
					}
					modifyFlag = !flag;
				}
			}
			if(modifyFlag){
				window.webDialogUtil.alert("提示","请修过后提交");
				return;
			}
			
			//基于ajaxfileUpload.js实现
			$.ajaxFileUpload({
				url : _this.urlItems.updateStoreURL,
				secureuri : false,
				data:entity,
				fileElementId : ['certPath','licensePath'],//现使用方法
				dataType : 'json',
				success : function(data) {
					if (data.code == _this.successCode) {
						
						 webAlert({
				                title:'提示',
				                content: data.msg,
				                opacity:.5,
				                bg:"gray",
				                width:300,
				                height:10,
				                button:[
				                        {
				                        name:"确定",
				                        callback:function(){
				                        	window.location = basePath+"/userCenter/ecommerceRecord/index" ;
				                        	this.close();
				                        }
				                        }]
				           });						
					} else {
						window.webDialogUtil.alert("提示",data.msg);
					}
				},
				error : function(data, status, e){
					window.webDialogUtil.alert("提示",data.msg);
				}
			});
		},
	
		
},

$(function(){	
	var busiDirtVal;
	$("#inputDiv input[type='radio']").each(function(){
		if($(this).prop("checked")){
			busiDirtVal = $(this).val();
		}				   
	});		
	ecommRecordEditCtrl.ecommRecordEdit.ecommObj = {
			id:$("#id").val(),
			customerCode : $("#customerCode").val(),
			customerName : $("#customerName").val(),
			companyAddr : $("#companyAddr").val(),
			postCode : $("#postCode").val(),
			busiIndt : $("#busiIndt").val(),
			busiDirt : busiDirtVal,
			salePrct : $("#salePrct").val(),
			purchPrct : $("#purchPrct").val(),
			license : $("#license").val(),
			agentCode : $("#agentCode").val(),
			agentName : $("#agentName").val(),
			appUId : $("#appUId").val(),
			appUName : $("#appUName").val(),
			ebpCode : $("#ebpCode").val(),
			ebpName : $("#ebpName").val(),				
			ebcCode : $("#ebcCode").val(),
			ebcName : $("#ebcName").val(),
			classifyCode : $("#classifyCode").val(),
			classifyName : $("#classifyName").val(),
			interfaceCode : $("#interfaceCode").val()
		}			
	ecommRecordEditCtrl.ecommRecordEdit.event();
	
});